﻿

using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;
using Gift.DAL;
using Gift.Model;

namespace WebApp.admin.message
{
    public partial class messageMail : BaseAdmin
    {

        tbMail oMail = new tbMail();


        // Methods
        protected void btnSendMail_Click(object sender, EventArgs e)
        {
            SendMailMessage message = new SendMailMessage();
            try
            {
                message.subject = "邮件测试";
                message.body = "邮件测试";
                message.html = true;
                message.mailTo = this.txtMailTo.Value;
                message.from = this.txtFromAddress.Value;
                message.fromName = this.txtFromName.Value;
                message.mailDomain = this.txtMailDomain.Value;
                message.mailDomainPort = Convert.ToInt32(this.txtMailDomainPort.Value);
                message.mailServerUserName = this.txtMailServerUserName.Value;
                message.mailServerPassWord = this.txtMailServerPassWord.Value;
                if (message.RuleSend())
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "send", "<script language=\"javascript\" defer>alert(\"发送成功！\");document.getElementById(\"txtMailServerPassWord\").value=\"" + message.mailServerPassWord + "\";document.getElementById(\"sendMail\").style.display=\"block\";document.getElementById(\"btnMailTest\").style.display=\"none\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "send", "<script language=\"javascript\" defer>alert(\"发送失败！\");document.getElementById(\"txtMailServerPassWord\").value=\"" + message.mailServerPassWord + "\";document.getElementById(\"sendMail\").style.display=\"block\";document.getElementById(\"btnMailTest\").style.display=\"none\";</script>");
                }
            }
            catch
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "send", "<script language=\"javascript\" defer>alert(\"发送失败！\");document.getElementById(\"txtMailServerPassWord\").value=\"" + message.mailServerPassWord + "\";document.getElementById(\"sendMail\").style.display=\"block\";document.getElementById(\"btnMailTest\").style.display=\"none\";</script>");
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
            try
            {
                this.oMail.fromAddress = this.txtFromAddress.Value;
                this.oMail.fromName = this.txtFromName.Value;
                this.oMail.mailDomain = this.txtMailDomain.Value;
                this.oMail.mailDomainPort = this.txtMailDomainPort.Value;
                this.oMail.mailServerUserName = this.txtMailServerUserName.Value;
                this.oMail.mailServerPassWord = this.txtMailServerPassWord.Value;
                this.oMail.isEnabled = Convert.ToBoolean(base.Request.Form["isEnabled"]);
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    this.oMail.id = Convert.ToInt32(base.Request.QueryString["id"]);
                    if (BaseFactory.theMail.Update(this.oMail))
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"mail_list.aspx\";</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");document.getElementById(\"txtMailServerPassWord\").value=\"" + this.oMail.mailServerPassWord + "\";</script>");
                    }
                }
                else if (BaseFactory.theMail.Add(this.oMail) > 0)
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"mail_list.aspx\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");document.getElementById(\"txtMailServerPassWord\").value=\"" + this.oMail.mailServerPassWord + "\";</script>");
                }
            }
            catch
            {
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");document.getElementById(\"txtMailServerPassWord\").value=\"" + this.oMail.mailServerPassWord + "\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");document.getElementById(\"txtMailServerPassWord\").value=\"" + this.oMail.mailServerPassWord + "\";</script>");
                }
            }
        }

        public StringBuilder GetTitle()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    builder.AppendLine("编辑邮件");
                    return builder;
                }
                builder.AppendLine("添加邮件");
            }
            catch
            {
            }
            return builder;
        }

        private void MailBind()
        {
            try
            {
                tbMail mailById = BaseFactory.theMail.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                if (mailById != null)
                {
                    this.txtFromAddress.Value = mailById.fromAddress;
                    this.txtFromName.Value = mailById.fromName;
                    this.txtMailDomain.Value = mailById.mailDomain;
                    this.txtMailDomainPort.Value = mailById.mailDomainPort;
                    this.txtMailServerUserName.Value = mailById.mailServerUserName;
                    base.ClientScript.RegisterStartupScript(base.ClientScript.GetType(), "ssm", "<script language=\"javascript\" defer>document.getElementById(\"txtMailServerPassWord\").value=\"" + mailById.mailServerPassWord + "\";</script>");
                    if (mailById.isEnabled)
                    {
                        this.rdIsEnabled1.Checked = true;
                    }
                    else
                    {
                        this.rdIsEnabled0.Checked = true;
                    }
                }
            }
            catch
            {
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
            //{
            //    base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
            //}
            if (!base.IsPostBack && (base.Request.QueryString["ot"] != null))
            {
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    this.MailBind();
                }
                else
                {
                    this.rdIsEnabled1.Checked = true;
                }
            }
        }

    }
}